package si.yun.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import si.yun.dao.LoginDao;
import si.yun.entity.SyLoginRecord;
import si.yun.entity.SyUser;
import si.yun.entity.User;
import si.yun.service.LoginService;
import si.yun.utils.AddressUtils;
import si.yun.utils.ipUtils;

import java.util.List;

/**
 * @ClassName LoginServiceImpl
 * @Author SXM
 * @Date 2021/07/28 14:19
 * @Version 1.0
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginDao dao;

    /*
     * @Author SXM
     * @Description //用户登录
     * @return si.yun.entity.SyUser
     * @Date 2021/7/28  19:15
     **/
    @Override
    @Transactional
    public SyUser selUser(String userName, String passWord,String ip,String cName) {
        SyUser user=dao.selUser(userName,passWord);
        if(user!=null){
            int num=addUserLog(user.getUserId(),ip,2,cName);
            if(num>0){
                return user;
            }else {
                System.out.println("日志添加失败");
            }
        }
        int num=addUserLog2(ip,3,cName);
        return user;
    }

    /*
     * @Author SXM
     * @Description //添加后台用户登录日志--登录成功
     * @return int
     * @Date 2021/7/30  15:06
     **/
    @Transactional
    public int addUserLog(Integer userId,String ip,int state,String cName){
        //String indirizzo = AddressUtils.indirizzo(ip);//报错是因为谷歌浏览器，换一个就好了
        User user=new User();
        user.setuId(userId);
        user.setIP(ip);
        user.setIndirizzo(cName);
        user.setState(state);
        int num=dao.addUserLog(user);
        return num;
    }
    /*
     * @Author SXM
     * @Description //添加后台用户登录日志--登录失败
     * @return int
     * @Date 2021/8/1  19:23
     **/
    @Transactional
    public int addUserLog2(String ip,int state,String cName){
        String indirizzo = AddressUtils.indirizzo(ip);
        int num=dao.addUserLog2(ip,indirizzo,state);
        return num;
    }

    /*
     * @Author SXM
     * @Description //账户设置
     * @return int
     * @Date 2021/7/29  15:31
     **/
    @Override
    public int updUser(User user) {
        return dao.updUser(user);
    }

    /*
     * @Author SXM
     * @Description //账户设置--图片提交
     * @return int
     * @Date 2021/8/26  19:51
     **/
    @Override
    public int updUserImg(Integer id, String imgName) {
        return dao.updUserImg(id,imgName);
    }

    /*
     * @Author SXM
     * @Description //查询登录日志
     * @return java.util.List<si.yun.entity.SyLoginRecord>
     * @Date 2021/7/30  15:21
     **/
    @Override
    public List<SyLoginRecord> selUserLog(Integer uId) {
        return dao.selUserLog(uId);
    }


}
